import api from '../../http/apis/apis'

export default {
    namespaced: true,
    state: {
        allInstances: {
            currentPage: 1,
            pageSize: 3,
            total: 0,
            totalPages: 0,
            data: []
        },
        update: {},
        InsuranceType: [],

    },

    mutations: {
        // 获取所有保险信息
        setAllInstances(state, newAllInstances) {
            state.allInstances = newAllInstances
        },
        setCurrent(state, newcurrent) {
            state.allInstances.currentPage = newcurrent
        },
        setPageSize(state, newpagesize) {
            console.log("new", newpagesize);
            state.allInstances.pageSize = newpagesize
        },
        setUpdate(state, newUpdate) {
            state.update = newUpdate
        },
        setInsuranceType(state, newInsuranceType) {
            state.InsuranceType = newInsuranceType
        }
    },

    //下面写请求
    actions: {
        //获取所有保险信息
        async getInsurance(context) {
            const { data } = await api.insurances.getInsurance({
                currentPage: context.state.allInstances.currentPage,
                pageSize: context.state.allInstances.pageSize,
            })
            console.log(data);
            if (data.status) {
                context.commit('setAllInstances', data.data);
            }
        },

        //新增保险
        async addInsurance(context, salesDate) {
            const { data } = await api.insurances.addInsurance(salesDate)
            console.log(data);
        },

        //删除保险
        async deleteInsurance(context, _id) {
            const { data } = await api.insurances.deleteInsurance({ _id })
            console.log(data);
            if (data.num) {
                context.dispatch("getInsurance")
            }
        },

        //通过ID获取保险信息
        async getInsuranceById(context, _id) {
            const { data } = await api.insurances.getInsuranceById({ _id })
            console.log(data);
            if (data.status) {
                context.commit('setUpdate', data.data)
            }
        },

        // 修改保险产品信息
        async modifyInsurance(context, val) {
            const { data } = await api.insurances.modifyInsurance(val)
            console.log(data);
            if (data.status) {
                context.dispatch("getInsurance")
            }
        },
        async lgetAllInsurances(context) {
            const { data } = await api.insurancestype.lgetAllInsurances()
            if (data.status) {
                context.commit('setInsuranceType', data.data)
            }
        },

    },



}